package com.example.myapplication.fixation;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.myapplication.KR;
import com.example.myapplication.NineFunSdkGiftbagActivity;
import com.example.myapplication.ResourceUtil;
import java.util.Timer;
import java.util.TimerTask;


/***
 *
 *类说明 ：悬浮窗小球
 *
 *
 */

public class GiftBagFloatView {

    private Context mContext;
    private View floatView;
    private WindowManager mManager;
    private WindowManager.LayoutParams params;
    private LinearLayout leftLayout,ll_csfloat_hint,ll_csfloat_right_hint;
    private ImageView imageview;
    private int mScreenWidth,mScreenHeigh;
    private boolean isDowned=true;
    private int mViewWidth,mViewheight;
    private static boolean existView=false;
    private TextView mTxtAccount , mTxtService , mTxtGame , mTxtHide ,mfloatHide;
    public static final int ACCOUNT_MANAGER = 0 ;
    public static final int GAME_SERVICE = 1 ;
    public static final int MORE_GAME = 2 ;
    public static final int HIDE_ICON = 3 ;
    public static final int FLOAT_MENU_LAYOUT = 4 ;
    private boolean isRegister=false;
    private boolean timerIsRun=false;
    private Timer timer;
    private boolean isLeft=true;



    public GiftBagFloatView(Context context, boolean register) {
        mContext=context;
        isRegister=register;
        initView();
    }

    public void setIsRegister(boolean register){
        isRegister=register;
        if(isRegister){
            setTimer();
        }
    }

    private void initView(){
        if(existView){
            return;
        }
        mScreenWidth = mContext.getResources().getDisplayMetrics().widthPixels;
        mScreenHeigh=mContext.getResources().getDisplayMetrics().heightPixels;
        mManager = (WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE);
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.LAST_APPLICATION_WINDOW,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,// FLAG_LAYOUT_NO_LIMITS使x轴可以超出手机屏幕外
                PixelFormat.TRANSLUCENT);

        params.gravity= Gravity.BOTTOM | Gravity.RIGHT | Gravity.CENTER_VERTICAL;
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        //需要根据横竖屏处理
/*        params.x=0;
//		params.y=mScreenHeigh/2-100;
        params.y = 50;*/
        findViewByid(1);
       // mHandler.postDelayed(run, 2000);
    }


    private int initialX;
    private int initialY;
    private float initialTouchX;
    private float initialTouchY;
    private int delay;
    class ImageviewOnTouch implements OnTouchListener {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    initialX = params.x;
                    initialY = params.y;
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    mContext.startActivity(new Intent(mContext, NineFunSdkGiftbagActivity.class));
                    break;
            }
            return true;
        }

    }

    private void updateView(){

        if(params.x>(mScreenWidth/2)){
            params.x=(mScreenWidth-mViewWidth/3);
            params.alpha = 0.5f;
            mManager.updateViewLayout(floatView, params);
        }else{
            params.x=(mViewWidth/3)-mViewWidth;
            params.alpha = 0.5f;
            mManager.updateViewLayout(floatView, params);
        }
        params.alpha = 1f;
    }

    private Handler mHandler=new Handler();
    Runnable run=new Runnable() {

        @Override
        public void run() {
            updateView();
        }
    };

    @SuppressLint("Range")
    private void findViewByid(int type){
        if(floatView!=null){
            mManager.removeView(floatView);
        }
        if(type==1){
            floatView= LayoutInflater.from(mContext).inflate(ResourceUtil.getLayoutId(mContext, KR.layout.giftbag_floatview_left), null, false);
            leftLayout=(LinearLayout)floatView.findViewById(ResourceUtil.getId(mContext, KR.id.ll_floatview_left_extends));
            ll_csfloat_hint=(LinearLayout)floatView.findViewById(ResourceUtil.getId(mContext, KR.id.ll_csfloat_hint));
            ll_csfloat_right_hint=(LinearLayout)floatView.findViewById(ResourceUtil.getId(mContext, KR.id.ll_csfloat_right_hint));
        }else{
            floatView= LayoutInflater.from(mContext).inflate(ResourceUtil.getLayoutId(mContext, KR.layout.cs_floatview_right), null, false);
            leftLayout=(LinearLayout)floatView.findViewById(ResourceUtil.getId(mContext, KR.id.ll_floatview_right_extends));
            ll_csfloat_hint=(LinearLayout)floatView.findViewById(ResourceUtil.getId(mContext, KR.id.ll_csfloat_right_hint));
        }

        floatView.setAlpha(100);
        imageview=(ImageView)floatView.findViewById(ResourceUtil.getId(mContext, KR.id.gift_floatview_right_button));
        leftLayout.setVisibility(View.GONE);
        ll_csfloat_hint.setVisibility(View.GONE);
        imageview.setOnTouchListener(new GiftBagFloatView.ImageviewOnTouch());
        imageview.getViewTreeObserver().addOnGlobalLayoutListener(
                new OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        mViewWidth = imageview.getWidth();
                        mViewheight=imageview.getHeight();
                        imageview.getViewTreeObserver()
                                .removeGlobalOnLayoutListener(this);
                    }
                });
        mManager.addView(floatView, params);

        setOnClickListener();
        existView=true;
        if(isRegister){
            setTimer();
        }
    }

    private void setOnClickListener(){
        mTxtAccount  = (TextView) floatView.findViewById(ResourceUtil.getId(mContext, KR.id.txt_floatmenu_account));
        mTxtService  = (TextView) floatView.findViewById(ResourceUtil.getId(mContext, KR.id.txt_floatmenu_service));
        mTxtGame  = (TextView) floatView.findViewById(ResourceUtil.getId(mContext, KR.id.txt_floatmenu_game));
        mTxtHide  = (TextView) floatView.findViewById(ResourceUtil.getId(mContext, KR.id.txt_floatmenu_hide));
        GiftBagFloatView.TextViewOnClick ciickLitener=new GiftBagFloatView.TextViewOnClick();
        mTxtAccount.setOnClickListener(ciickLitener);
        mTxtGame.setOnClickListener(ciickLitener);
        mTxtHide.setOnClickListener(ciickLitener);
        mTxtService.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Toast.makeText(mContext,"客服", Toast.LENGTH_SHORT).show();
            }
        });

    }

    class TextViewOnClick implements OnClickListener {

        @Override
        public void onClick(View v) {

            Toast.makeText(mContext,""+v.getId(), Toast.LENGTH_SHORT).show();

        }

    }

    public void hideFloatView(){
        if(mManager!=null&&floatView!=null){
            mHandler.removeCallbacks(run);
//			mManager.removeView(floatView);
            mManager.removeViewImmediate(floatView);
            mManager=null;
            existView=false;
        }
    }

    private void setTimer(){
        if(timerhandler!=null){
            timerhandler.removeMessages(1);
            timerhandler.removeMessages(2);
        }if(timer==null){
            timer=new Timer();
        }else{
            timer.cancel();
            timer=new Timer();
        }
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                timerhandler.sendEmptyMessage(1);
            }
        }, 5000);
    }

    private Handler timerhandler=new Handler(){
        public void handleMessage(Message msg) {
            if(msg.what==1){

                if(leftLayout!=null&&leftLayout.getVisibility()== View.GONE&&ll_csfloat_hint!=null&&ll_csfloat_hint.getVisibility()== View.GONE){

                    ll_csfloat_hint.setVisibility(View.VISIBLE);
                    timerhandler.sendEmptyMessageDelayed(2, 3000);
                    isRegister=false;
                }

            }else if(msg.what==2){
                ll_csfloat_hint.setVisibility(View.GONE);

            }

        };
    };





}
